//
//  TimeChooseViewController.m
//  Cheirapsis
//
//  Created by wangning on 15/11/3.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "TimeChooseViewController.h"
#import "FillOutViewController.h"
#import "ServeProjectViewController.h"
#import "Define.h"

@interface TimeChooseViewController (){
    NSMutableArray *dateArray;
    NSTimeInterval secondsPerDay;
    NSDate *today;
    NSDate *tomorrow,*afterTomorrow;
}

@end

@implementation TimeChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dateSet];
    [self dataLoad];
    self.title = @"选择时间";

    _type = TodayType;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)dateSet{
    secondsPerDay = 24*60*60;
    today = [NSDate date];
    tomorrow = [today dateByAddingTimeInterval:secondsPerDay];
    afterTomorrow = [today dateByAddingTimeInterval:2*secondsPerDay];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"identifier%ld",(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
    NSDate *nowDate = [NSDate date];
    [dateFor setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    for (int i = 0; i < 12; i++) {
        NSDictionary *dic = [dateArray objectAtIndex:i];
        NSDate *needForDate = [dateFor dateFromString:dic[@"time"]];
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needForDate];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((80*(i%4)+5)*F_WIDTH, (80*(i/4)+5)*F_HEIGHT, 70*F_WIDTH, 70*F_HEIGHT)];
        btn.tag = 1000+i;
        [btn setBackgroundImage:[UIImage imageNamed:dic[@"btnImage"]] forState:UIControlStateNormal];
        [btn setTitle:dic[@"date"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:11*F_WIDTH];
        if (time > 0) {
            [btn setBackgroundImage:[UIImage imageNamed:@"蓝色边框"] forState:UIControlStateNormal];
        }else if (time < 0){
            [btn setBackgroundImage:[UIImage imageNamed:@"蓝色背景"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(dateChoose:) forControlEvents:UIControlEventTouchUpInside];
        }
        [cell.contentView addSubview:btn];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300*F_HEIGHT;
}

- (void)dateChoose:(UIButton *)btn{
    FillOutViewController *fillOut = [[FillOutViewController alloc]init];
    fillOut.dateString = [NSString stringWithFormat:@"%@",dateArray[btn.tag -1000][@"arrivetime"]];
    fillOut.k = _k;
    fillOut.dic = _dic;
    [self.navigationController pushViewController:fillOut animated:YES];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 175)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(35 * F_WIDTH, 15 * F_HEIGHT, 256 * F_WIDTH, 50*F_HEIGHT)];
    imageView.image = [UIImage imageNamed:@"时间选择"];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 75*F_HEIGHT, WIDTH, 40*F_HEIGHT)];
    label.text = @"请选择预约时间，白色表示不可预约(提前半小时)";
    label.font = [UIFont systemFontOfSize:13*F_WIDTH];
    label.backgroundColor = ALL233COROL;
    label.textAlignment = NSTextAlignmentCenter;
    
   
    
    NSString *todayString = [[[today description] substringToIndex:10] substringFromIndex:5];
    NSString *tomorrowString = [[[tomorrow description] substringToIndex:10] substringFromIndex:5];
    NSString *afterTomorrowString = [[[afterTomorrow description] substringToIndex:10] substringFromIndex:5];
    
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((85*i+40)*F_WIDTH, 120*F_HEIGHT, 75*F_WIDTH, 50*F_HEIGHT)];
        btn.tag = 1000+i;
        if (_type == TodayType) {
            if (i == 0){
                [btn setBackgroundImage:[UIImage imageNamed:@"day_01"] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }if (i == 1) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_12"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }if (i == 2) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_13"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }
        }else if (_type == TomorrwoType) {
            if (i == 0){
                [btn setBackgroundImage:[UIImage imageNamed:@"day_11"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }if (i == 1) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_02"] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }if (i == 2) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_13"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }
        }else if (_type == AfterTomType){
            if (i == 0){
                [btn setBackgroundImage:[UIImage imageNamed:@"day_11"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }if (i == 1) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_12"] forState:UIControlStateNormal];
                [btn setTitleColor:MAINCOROL forState:UIControlStateNormal];
            }if (i == 2) {
                [btn setBackgroundImage:[UIImage imageNamed:@"day_03"] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
        }
        
        btn.titleLabel.lineBreakMode = 0;
        [btn addTarget:self action:@selector(dataChange:) forControlEvents:UIControlEventTouchUpInside];
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
        if (i == 0) {
            [btn setTitle:[NSString stringWithFormat:@"%@",todayString] forState:UIControlStateNormal];
        }else if(i ==1){
            [btn setTitle:[NSString stringWithFormat:@"%@",tomorrowString] forState:UIControlStateNormal];
        }else if(i ==2){
            [btn setTitle:[NSString stringWithFormat:@"%@",afterTomorrowString] forState:UIControlStateNormal];
        }
        [headerView addSubview:btn];
    }
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 174*F_HEIGHT, WIDTH, 1)];
    lineView.backgroundColor = ALL233COROL;
    
    [headerView addSubview:imageView];
    [headerView addSubview:label];
    [headerView addSubview:lineView];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 175*F_HEIGHT;
}

- (void)dataChange:(UIButton *)btn{
    
    if (btn.tag == 1000) {
        [self dataLoad];
        [_tableView reloadData];
    }else if (btn.tag == 1001) {
        _type = TomorrwoType;
        NSString *tomorrowString = [[tomorrow description] substringToIndex:11];
        dateArray = [NSMutableArray arrayWithArray:
                     @[
                       @{
                           @"date":@"8:00~9:00",
                           @"time":[NSString stringWithFormat:@"%@07:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@08:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"9:00~10:00",
                           @"time":[NSString stringWithFormat:@"%@08:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@09:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"10:00~11:00",
                           @"time":[NSString stringWithFormat:@"%@09:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@10:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"11:00~12:00",
                           @"time":[NSString stringWithFormat:@"%@10:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@11:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"12:00~13:00",
                           @"time":[NSString stringWithFormat:@"%@11:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@12:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"13:00~14:00",
                           @"time":[NSString stringWithFormat:@"%@12:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@13:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"14:00~15:00",
                           @"time":[NSString stringWithFormat:@"%@13:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@14:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"15:00~16:00",
                           @"time":[NSString stringWithFormat:@"%@14:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@15:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"16:00~17:00",
                           @"time":[NSString stringWithFormat:@"%@15:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@16:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"17:00~18:00",
                           @"time":[NSString stringWithFormat:@"%@16:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@17:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"18:00~19:00",
                           @"time":[NSString stringWithFormat:@"%@17:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@18:00:00",tomorrowString]
                           },
                       @{
                           @"date":@"19:00~20:00",
                           @"time":[NSString stringWithFormat:@"%@18:59:59",tomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@19:00:00",tomorrowString]
                           }
                       ]
                     ];
        [_tableView reloadData];
    }else if (btn.tag == 1002){
        _type = AfterTomType;
        NSString *afterTomorrowString = [[afterTomorrow description] substringToIndex:11];
        dateArray = [NSMutableArray arrayWithArray:
                     @[
                       @{
                           @"date":@"8:00~9:00",
                           @"time":[NSString stringWithFormat:@"%@07:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@08:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"9:00~10:00",
                           @"time":[NSString stringWithFormat:@"%@08:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@09:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"10:00~11:00",
                           @"time":[NSString stringWithFormat:@"%@09:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@10:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"11:00~12:00",
                           @"time":[NSString stringWithFormat:@"%@10:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@11:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"12:00~13:00",
                           @"time":[NSString stringWithFormat:@"%@11:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@12:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"13:00~14:00",
                           @"time":[NSString stringWithFormat:@"%@12:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@13:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"14:00~15:00",
                           @"time":[NSString stringWithFormat:@"%@13:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@14:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"15:00~16:00",
                           @"time":[NSString stringWithFormat:@"%@14:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@15:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"16:00~17:00",
                           @"time":[NSString stringWithFormat:@"%@15:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@16:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"17:00~18:00",
                           @"time":[NSString stringWithFormat:@"%@16:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@17:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"18:00~19:00",
                           @"time":[NSString stringWithFormat:@"%@17:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@18:00:00",afterTomorrowString]
                           },
                       @{
                           @"date":@"19:00~20:00",
                           @"time":[NSString stringWithFormat:@"%@18:59:59",afterTomorrowString],
                           @"arrivetime":[NSString stringWithFormat:@"%@19:00:00",afterTomorrowString]
                           }
                       ]
                     ];
        [_tableView reloadData];
    }
}

- (void)dataLoad{
    _type = TodayType;
    NSString *todayString = [[today description] substringToIndex:11];
    dateArray = [NSMutableArray arrayWithArray:
                 @[
                   @{
                       @"date":@"8:00~9:00",
                       @"time":[NSString stringWithFormat:@"%@07:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@08:00:00",todayString]
                       },
                   @{
                       @"date":@"9:00~10:00",
                       @"time":[NSString stringWithFormat:@"%@08:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@09:00:00",todayString]
                       },
                   @{
                       @"date":@"10:00~11:00",
                       @"time":[NSString stringWithFormat:@"%@09:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@10:00:00",todayString]
                       },
                   @{
                       @"date":@"11:00~12:00",
                       @"time":[NSString stringWithFormat:@"%@10:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@11:00:00",todayString]
                       },
                   @{
                       @"date":@"12:00~13:00",
                       @"time":[NSString stringWithFormat:@"%@11:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@12:00:00",todayString]
                       },
                   @{
                       @"date":@"13:00~14:00",
                       @"time":[NSString stringWithFormat:@"%@12:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@13:00:00",todayString]
                       },
                   @{
                       @"date":@"14:00~15:00",
                       @"time":[NSString stringWithFormat:@"%@13:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@14:00:00",todayString]
                       },
                   @{
                       @"date":@"15:00~16:00",
                       @"time":[NSString stringWithFormat:@"%@14:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@15:00:00",todayString]
                       },
                   @{
                       @"date":@"16:00~17:00",
                       @"time":[NSString stringWithFormat:@"%@15:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@16:00:00",todayString]
                       },
                   @{
                       @"date":@"17:00~18:00",
                       @"time":[NSString stringWithFormat:@"%@16:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@17:00:00",todayString]
                       },
                   @{
                       @"date":@"18:00~19:00",
                       @"time":[NSString stringWithFormat:@"%@17:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@18:00:00",todayString]
                       },
                   @{
                       @"date":@"19:00~20:00",
                       @"time":[NSString stringWithFormat:@"%@18:59:59",todayString],
                       @"arrivetime":[NSString stringWithFormat:@"%@19:00:00",todayString]
                       }
                   ]
                 ];
}

@end
